//
//  LTProfileViewController.m
//  LTChat
//
//  Created by 俊松何 on 2017/9/12.
//  Copyright © 2017年 俊松何. All rights reserved.
//

#import "LTProfileViewController.h"
#import "LTProfileDetailController.h"
#import "LTSetViewController.h"
#import "MTWalletViewController.h"


@interface LTProfileViewController ()<LTContactManagerDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *avaterImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLable;
@property (weak, nonatomic) IBOutlet UILabel *accountLable;


@property (weak, nonatomic) IBOutlet UILabel *walletLable;
@property (weak, nonatomic) IBOutlet UILabel *numberWalletLable;

@property (weak, nonatomic) IBOutlet UILabel *albumLable;
@property (weak, nonatomic) IBOutlet UILabel *bqLable;


@property (weak, nonatomic) IBOutlet UILabel *setLable;
@property (weak, nonatomic) IBOutlet UILabel *invateLable;

@property (nonatomic,strong) UIButton *titleButton;

@property(nonatomic,strong) MTUserModel *model;

@end

@implementation LTProfileViewController

+(instancetype)profileVc
{
    UIStoryboard* storyBoard = [UIStoryboard storyboardWithName:@"Me" bundle:nil];
    LTProfileViewController *Vc = [storyBoard instantiateViewControllerWithIdentifier:@"LTProfileViewControllerID"];
    return Vc;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupData];
    
    [[LTClient sharedClient].contactManager addDelegate:self];

    self.navigationItem.title = nil;
    _titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_titleButton setTitle:NSLTLocalizedString(@"mtmy.title", nil) forState:UIControlStateNormal];
    [_titleButton sizeToFit];
    _titleButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
    [_titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:_titleButton];
    self.navigationItem.leftMargin = 15;
    
    @weakify(self);
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:UserInfoChange object:nil] subscribeNext:^(NSNotification * _Nullable x) {
        @strongify(self);
        [self curentUserInfoChange];
    }];
    
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
    [self curentUserInfoChange];
}


#pragma mark LTContactManagerDelegate
-(void)curentUserInfoChange
{
    [self.avaterImageView sd_setImageWithURL:[NSURL URLWithString:[LTClient sharedClient].currentUser.avatar] placeholderImage:defaultImgae];
    self.nameLable.text = [LTClient sharedClient].currentUser.name;
    self.accountLable.text = [NSString stringWithFormat:@"账号:%@",[LTClient sharedClient].currentUser.account];
}

-(void)setupData
{
    
    self.walletLable.text =   NSLTLocalizedString(@"me.jifen", nil);
    self.numberWalletLable.text = NSLTLocalizedString(@"me.numberWallet", nil);
    self.albumLable.text = NSLTLocalizedString(@"discover.scan", nil);
    self.bqLable.text = NSLTLocalizedString(@"me.expression", @"expression");
    self.setLable.text = NSLTLocalizedString(@"me.setting", @"set");
    self.invateLable.text = NSLTLocalizedString(@"me.invate", @"set");
    [_titleButton setTitle:NSLTLocalizedString(@"mtmy.title", nil) forState:UIControlStateNormal];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == 0)
    {
        CGFloat headerH = 1;
        return headerH;
    }else
    {
        return 15;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 1)
    {
        CGFloat cellH = 60;
        return cellH;
    }else if(indexPath.section == 4)
    {
        CGFloat cellH = 60;
        return cellH;
    }else
    {
        return  [super tableView:tableView heightForRowAtIndexPath:indexPath];
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.section == 0)
    {

//        MTProfileViewController *profileVc = [[MTProfileViewController alloc] init];
//        profileVc.userId = [LTClient sharedClient].currentUserId;
//        [self.navigationController pushViewController:profileVc animated:YES];
        
    }else if (indexPath.section == 1)
    {
        MTWalletViewController *coinVc = [[MTWalletViewController alloc] init];
        [self.navigationController pushViewController:coinVc animated:YES];
        
    }else if (indexPath.section == 2)
    {
        switch (indexPath.row) {
            case 0:{
                [self scanClick];
            } break;
            case 1:{
               
            } break;
            case 2:{
               
                [[MTUserApiManager manager] getUserAuthenticationinfoWithcompletion:^(UserAuthenticationinfo *info, MTError *mterror) {
                    
                    if(mterror == nil)
                    {
                        if(info.status == AuthenticationStatusPass)
                        {
                            [MBProgressHUD showMessage:@"已经认证成功"];
                            
                        }else if(info.status == AuthenticationStatusNone)
                        {
                            MTWebViewController *webVc = [[MTWebViewController alloc] init];
                            webVc.url = info.authenticationUrl;
                            MTNavigationController *navVc = [[MTNavigationController alloc] initWithRootViewController:webVc];
                            [self presentViewController:navVc animated:YES completion:nil];
                            
                        }else if(info.status == AuthenticationStatusWaitAuth)
                        {
                            UIAlertController *alterVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"实名认证审核中" preferredStyle:UIAlertControllerStyleAlert];
                            UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                                
                            }];
                            [alterVc addAction:action1];
                            [self presentViewController:alterVc animated:YES completion:nil];
                            
                        }else if (info.status == AuthenticationStatusNoPass)
                        {
                            UIAlertController *alterVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"实名认证失败，请重新认证" preferredStyle:UIAlertControllerStyleAlert];
                            UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                                MTWebViewController *webVc = [[MTWebViewController alloc] init];
                                webVc.url = info.authenticationUrl;
                                MTNavigationController *navVc = [[MTNavigationController alloc] initWithRootViewController:webVc];
                                [self presentViewController:navVc animated:YES completion:nil];
                            }];
                            [alterVc addAction:action1];
                            [self presentViewController:alterVc animated:YES completion:nil];
                            
                        }
                        
                    }
                    
                }];
                
            } break;
            default:break;
        }
    }else if (indexPath.section == 3)
    {
        LTSetViewController *Vc = [LTSetViewController baseStoryBordWithName:@"Me"];
        [self.navigationController pushViewController:Vc animated:YES];
    }else if (indexPath.section == 4)
    {
//        LTInvateController *invateVc = [LTInvateController baseStoryBordWithName:@"Me"];
//        [self.navigationController pushViewController:invateVc animated:YES];
    }
}


-(void)scanClick
{
   
    
    
}



@end
